var crypto=require('crypto')

const writeData = '123456';

const publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVjuVnj34QuRwmBxVCU1Fk+IuY
CAANchHQGeDnnAbGUWYOIsIKCMVCDYlQqL0GL6sC5Jpx3M6GSQUgxL1SR2hYqOva
xfUCYRKlRcLBbraCqlc7rC+FrFLxctLS7POks/lJSEGXkM7wrka73V+w2nvTx52+
5RpFzbn7XOFo4cvzLwIDAQAB
-----END PUBLIC KEY-----
`;

const privateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDVjuVnj34QuRwmBxVCU1Fk+IuYCAANchHQGeDnnAbGUWYOIsIK
CMVCDYlQqL0GL6sC5Jpx3M6GSQUgxL1SR2hYqOvaxfUCYRKlRcLBbraCqlc7rC+F
rFLxctLS7POks/lJSEGXkM7wrka73V+w2nvTx52+5RpFzbn7XOFo4cvzLwIDAQAB
AoGAbwvU4drNdKisqGF6fniQlG5hN/bBADzWYl1ygzywkSL2bFdKiqkw2T3srxJu
5+6Vf/WbEaX9hfomPnnO8B/Nc4iqwVqocIK2P8/7IlcqbRvFTOOJWcX0W3ubo54y
jf5HGTyqbeO5lT7E+7TdGP6GUSjYpdWYxZI6Yya3sy3ZDuECQQD+ZgF/ivhwlUZD
DSoioUX9NgQcvDkZ/kilOdJtaQFT1tqQPFqLUqFGLRVME9lUWFRfkJw5ZHShbt0J
3zFtSFAHAkEA1ucSOMYN4dXUonDxOxHTDbfgVnI4+FR/k5hgcenFXpBTBAQjLaoo
QCI52jvEU2BkzfpdagTpWs8kYsNHxmUpmQJBAPCJZnU/4zQgEHUpp2Ni9Zg7UJwS
5DLNmcaP64UDufuZtMKYLkx5bn+59N8gDWeQtQY+51fkwdo5xsmkvUlFDFUCQQCX
r03LSPAvgTCu361FllU0D5+HJORVU78gvKMGhIzNLG2MHid+aeYzLnVLNnhiqV09
jgQ1evYDJMZ394JH5MuxAkEAqtJf9AiKzI+hJtEKTuvhDC1UROyyocny6fcw5hBJ
hjHY8MCRdWUvmwChK0N1uhM/z3lHa8fwyQRREgUSDPeB+A==
-----END RSA PRIVATE KEY-----
`;

//加密
const publicKeyData = crypto.publicEncrypt(publicKey, Buffer.from(writeData));
const readData = crypto.privateDecrypt(privateKey, publicKeyData).toString();

console.log('加密后的密文：');
console.log(publicKeyData.toString('hex'));
// console.log(JSON.stringify(publicKeyData.toJSON()));
console.log(readData);
